starter.controller('stopCheckTaskController', ['ApiEndpoint','localStore','$rootScope','$window','$state','$ionicHistory','$timeout','$ionicScrollDelegate', '$scope', 'XCode','Page',
    function(ApiEndpoint,localStore,$rootScope,$window,$state,$ionicHistory,$timeout,$ionicScrollDelegate, $scope, XCode, Page) {

        $scope.task = {};
        $scope.task.taskType = '2';
        $scope._pageSize = 15;
        $scope._pageIndex = 0;

        $scope.screenWidth = document.body.clientWidth;
        $scope.screenHeight = window.innerHeight;


        $scope.vm = {
            moredata: false,
            pagination: {
                _pageSize: 15,
                _pageIndex: 0
            },
            descText : '上拉加载更多',
            init: function () {
                $scope.taskItems = [];
                angular.extend($scope.taskItems,$scope.taskItemList);
            },
            loadMore: function () {
                //if($scope.taskItemList == undefined || $scope.taskItemList.length < 15) {
                //    return;
                //}
                $timeout(function () {
                    $scope.vm.pagination._pageIndex += 1;
                    if($scope.vm.pagination._pageSize == 15) {
                        $scope.vm.pagination._pageSize = $scope.vm.pagination._pageSize - 15 + 5;
                    } else {
                        $scope.vm.pagination._pageSize = 5;
                    }

                    if($scope.vm.moredata) {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                        return;
                    } else {
                        if($scope.taskItems == undefined || $scope.taskItems.length == 0) {
                            $scope.$broadcast('scroll.infiniteScrollComplete');
                            return;
                        }
                        Page.ajaxPostQuiet(ApiEndpoint.mobileQueryItemUrl,{
                                '_pageSize': $scope.vm.pagination._pageSize,
                                '_pageIndex': $scope.vm.pagination._pageIndex,
                                taskId : $scope.searchData.taskId,
                                taskType : $scope.task.taskType
                            },
                            function (resp) {
                                if(resp.data.rows.length == 0) {
                                    $scope.vm.moredata = true;
                                    $scope.vm.descText = "没有更多数据";
                                    $scope.$broadcast('scroll.infiniteScrollComplete');
                                    return;
                                }
                                $scope.vm.descText = "上拉加载更多";
                                for (var i = 0; i < resp.data.rows.length; i++) {
                                    $scope.taskItems.push(resp.data.rows[i]);
                                }
                                $scope.$broadcast('scroll.infiniteScrollComplete');
                            });
                    }
                }, 1500);

            }

        };



        Page.ajaxPostQuiet(ApiEndpoint.mobileQueryUrl, {taskType : $scope.task.taskType}, function (resp) {
            $scope.taskDatas = resp.data;
            if(resp.data.length > 0) {
                var obj = localStore.getObject("selectStopCheckSelectData");
                if(obj == null || angular.equals({}, obj)) {
                    $scope.searchData = resp.data[0];
                } else {
                    $scope.searchData = obj;
                }
                //$scope.vm.init();
                queryTaskItem($scope.searchData);
            } else {
                $scope.searchData = {}
            }

        });
        //
        function queryTaskItem(searchData) {
            localStore.setObject("selectStopCheckSelectData",searchData);
            Page.ajaxPostQuiet(ApiEndpoint.mobileQueryItemUrl, {taskId : searchData.taskId, _pageSize : $scope._pageSize,
                _pageIndex : $scope._pageIndex,taskType : $scope.task.taskType}, function (resp) {
                $scope.taskItemList = resp.data.rows;
                if($scope.taskItemList.length >= 15) {
                    //$scope.init($scope._pageSize, $scope._pageIndex);
                    $scope.vm.moredata = false;
                    $scope.vm.descText = "上拉加载更多";
                    $scope.vm.pagination._pageSize = 15;
                    $scope.vm.pagination._pageIndex = 0;
                    $scope.vm.init();
                }
            });
        }

        $scope.stopCheckSelect = function(){
            var selectBusCheck = new IosSelect(1,
                [$scope.taskDatas],
                {
                    container: '.stopCheckContainer',
                    title: '检查任务',
                    itemHeight: 30,
                    itemShowCount: $scope.taskDatas.length,
                    oneLevelId:$scope.searchData.taskId,
                    callback: function (selectOneObj) {
                        var searchData = {};
                        searchData.taskId = selectOneObj.taskid;
                        searchData.taskTitle = selectOneObj.tasktitle;
                        $scope.searchData = searchData;
                        queryTaskItem(searchData);
                    }
                });
        };

        $scope.clickItem = function(item) {
            item.taskType = $scope.task.taskType;
            localStore.setObject("selectItem",item);
            $state.go("check_detail");
        }


    }]);
